package com.sgb.kjwl.view.ui.activity.manage

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.Html
import android.text.TextUtils
import android.view.View
import androidx.databinding.DataBindingUtil
import com.sgb.kjwl.AppActivity
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.ActivityWebsiteGroupBinding
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageComponent
import com.sgb.kjwl.di.manage.EnterpriseManageModule
import com.sgb.kjwl.model.entity.manage.GroupDetailEntity
import com.sgb.kjwl.utils.ReplaceUtils
import com.swgk.core.event.EventConstant
import com.sgb.kjwl.view.event.self.EditMessageEvent
import com.sgb.kjwl.view.ui.adapter.manage.TagAdapter
import com.sgb.kjwl.view.widget.manage.MImgGetter
import com.sgb.kjwl.viewmodel.manage.WebSiteGroupModel
import com.swgk.core.base.di.AppComponent
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.*
import javax.inject.Inject

/**
 * 团队----信息简介
 */
class WebSiteGroupActivity : AppActivity() {

    @Inject
    lateinit var mWebSiteModel : WebSiteGroupModel

    private lateinit var mBinging : ActivityWebsiteGroupBinding

    private var mGroupDetailEntity : GroupDetailEntity ?= null

    private lateinit var mTagAdapter : TagAdapter

    companion object {
        fun start(context : Context?){
            val intent = Intent(context, WebSiteGroupActivity::class.java)
            context!!.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
    }

    override fun setupActivityComponent(appComponent: AppComponent?) {
        DaggerEnterpriseManageComponent.builder().appComponent(appComponent)
            .enterpriseManageModule(EnterpriseManageModule(this)).build().inject(this)
    }

    override fun init() {
       mWebSiteModel.getGroupDetail()
        mBinging.tvManageBusiness.setEnable(false)
    }

    override fun onResume() {
        super.onResume()
        mWebSiteModel.mGroupDetailEntity.observe(this, {
            if (it != null){
                mGroupDetailEntity = it
                renderView(mGroupDetailEntity!!)
            }
        })
    }
    override fun initView() {
        mBinging = DataBindingUtil.setContentView(this, R.layout.activity_website_group)
        mBinging.include.titleView.setText(R.string.self_group_msg)
        mBinging.include.ivBack.setOnClickListener(this)
        mBinging.tvUpdate.setOnClickListener(this)
        initRefreshView()
        mTagAdapter = TagAdapter(this, R.layout.item_tag_group_info, ArrayList())
        //mBinging.recyclerViewTag.adapter = mTagAdapter
    }

    fun renderView(entity : GroupDetailEntity){
        mBinging.viewModel = entity
        mBinging.tvGroupAddress.text = if (TextUtils.isEmpty(entity.businessAddress))  "-" else entity.businessAddress
        mBinging.tvManageBusiness.editText.setText(if (TextUtils.isEmpty(entity.newMainBusinessName))  "-" else entity.newMainBusinessName)
        //setAvatarView(entity.logo)
        if (TextUtils.isEmpty(entity.teamDes)){
            mBinging.tvDescribe.text = "--"
        }else{
            val des = ReplaceUtils.getHtml(entity.teamDes)
            mBinging.tvDescribe.text = Html.fromHtml(des, MImgGetter(this, mBinging.tvDescribe), null)
        }


        if (entity.serviceCityName.isNotEmpty()){
            mBinging.tvServiceCity.text = entity.serviceCityName.joinToString(",")
        } else if (entity.cityDTOS != null && entity.cityDTOS!!.isNotEmpty()) {
            val sb = StringBuilder("")
            entity.cityDTOS!!.map {
                sb.append(it.areaName).append(",")
            }
            mBinging.tvServiceCity.text = sb.substring(0, sb.length - 1)
        }  else {
            mBinging.tvServiceCity.text = "-"
        }
        if (!TextUtils.isEmpty(entity.businessType)) {
            mTagAdapter.data = entity.businessType.split(",")
        }
    }

    /**
     * 显示团队图像
     * @param logo
     */
//    private fun setAvatarView(logo: String) {
//        if (!TextUtils.isEmpty(logo)) {
//            val options = RequestOptions()
//                .placeholder(R.drawable.icon_team_default) //图片加载出来前，显示的图片
//                .error(R.drawable.icon_team_default) //图片加载失败后，显示的图片
//            Glide.with(this).load(logo).apply(options).into(mBinging.ivGroup)
//        }
//    }

    private fun initRefreshView() {
        mBinging.swipeRefresh.setEnableLoadMore(false)
        //mBinging.swipeRefresh.setEnableRefresh(false)
        // 刷新
        mBinging.swipeRefresh.setOnRefreshListener {
            it.finishRefresh(500)
        }
    }

    //点击事件处理
    override fun onClick(v: View?) {
        super.onClick(v)
        when (v!!.id){
            R.id.iv_back -> {
                finish()
            }
            R.id.tv_update -> {
                WebSiteEditGroupActivity.start(this, mGroupDetailEntity)
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
     fun onGetMessage(event : EditMessageEvent){
         if (event.messageTag == EventConstant.EVENT_EDIT_MESSAGE_SUCCESS){
             mWebSiteModel.getGroupDetail();
//             mGroupDetailEntity = event.messageData as GroupDetailEntity
//             mWebSiteModel.mGroupDetailEntity.postValue(mGroupDetailEntity)
         }
    }



    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

}